// Project: op_filter
// FileName: op_filter.c
#include "./USB/usb.h"
#include "./USB/usb_function_cdc.h"
#include "HardwareProfile.h"
#include "Compiler.h"
#include "usb_config.h"
#include "USB\usb_device.h"
#include "USB\usb.h"
#include <delays.h>
/** CONFIGURATION **************************************************/
#pragma config CPUDIV = NOCLKDIV
#pragma config USBDIV = OFF
#pragma config FOSC = HS
#pragma config PLLEN = ON
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRTEN = OFF
#pragma config BOREN = OFF
#pragma config BORV = 30
#pragma config WDTEN = OFF
#pragma config WDTPS = 32768
#pragma config MCLRE = OFF
#pragma config HFOFST = OFF
#pragma config STVREN = ON
#pragma config LVP = OFF
#pragma config XINST = OFF
#pragma config BBSIZ = OFF
#pragma config CP0 = OFF
#pragma config CP1 = OFF
#pragma config CPB = OFF
#pragma config WRT0 = OFF
#pragma config WRT1 = OFF
#pragma config WRTB = OFF
#pragma config WRTC = OFF
#pragma config EBTR0 = OFF
#pragma config EBTR1 = OFF
#pragma config EBTRB = OFF
/** V A R I A B L E S ********************************************************/
#pragma udata
char input_buffer[64];
char bufn[64];
unsigned int n, i, j;
//const unsigned char weight[] = {
//63, 28, 18, 13, 10, 8, 7, 6, 5, 5,
//4, 4, 4, 3, 3, 3, 3, 2, 2, 2
//};
unsigned char a1,a2,a3,a4,a5,a6,a7,a8,a9,a10;
unsigned char a11,a12,a13,a14,a15,a16,a17,a18,a19,a20;
unsigned char ssw;
#define Delay_50uS Delay100TCYx(6)
#define Delay_1mS Delay1KTCYx(12)
#define Delay_2mS Delay1KTCYx(24)
#define Delay_5mS Delay10KTCYx(6)
#define Delay_10mS Delay10KTCYx(12)
#define Delay_20mS Delay10KTCYx(24)
#define Delay_100mS Delay10KTCYx(120)
/** P R I V A T E P R O T O T Y P E S ***************************************/
static void InitializeSystem(void);
void ProcessIO(void);
void USBDeviceTasks(void);
void YourHighPriorityISRCode();
void YourLowPriorityISRCode();
void UserInit(void);
/** VECTOR REMAPPING ***********************************************/
#define REMAPPED_RESET_VECTOR_ADDRESS 0x00
#define REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS 0x08
#define REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS 0x18
#pragma code REMAPPED_HIGH_INTERRUPT_VECTOR = REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS
void Remapped_High_ISR (void){
_asm goto YourHighPriorityISRCode _endasm
}
#pragma code REMAPPED_LOW_INTERRUPT_VECTOR = REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS
void Remapped_Low_ISR (void){
_asm goto YourLowPriorityISRCode _endasm
}
#pragma code
#pragma interrupt YourHighPriorityISRCode
void YourHighPriorityISRCode(){
USBDeviceTasks(); //USB_POLLING
}
#pragma interruptlow YourLowPriorityISRCode
void YourLowPriorityISRCode(){
}
#pragma code
void main(void){
InitializeSystem();
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
TRISA = 0b00000000;
TRISB = 0b00010000;
TRISC = 0b00000000;
ANSEL = 0b00000000; //ADコンバーター使用/不使用に関わらずANSEL設定
ANSELH = 0b00000000;
while(1){
if(USB_BUS_SENSE && (USBGetDeviceState() == DETACHED_STATE)){
USBDeviceAttach();
}
USBDeviceTasks();
ProcessIO();
}
}
static void InitializeSystem(void){
UserInit();
USBDeviceInit();
}
void UserInit(void){
//Initialize all of the LED pins
mInitAllLEDs();
//Initialize the pushbuttons
mInitAllSwitches();
n = 0;
}
void ProcessIO(void){
if(getsUSBUSART(input_buffer,32)){
switch(input_buffer[0]){
case '0':
for(ssw=0; ssw<2; ssw++){
if(USBUSARTIsTxTrfReady()) putrsUSBUSART("OK");
CDCTxService();
}
break;
case '1':
for(i=0; i<63; i++) bufn[i] = '0';
a1=0; a2=0; a3=0; a4=0; a5=0; a6=0; a7=0; a8=0; a9=0; a10=0;
a11=0; a12=0; a13=0; a14=0; a15=0; a16=0; a17=0; a18=0; a19=0; a20=0;
for(i=0; i<55000; i++){
if(PORTBbits.RB4) n++;
else{
if((2000 < n) && (n < 10001)) a1++;
if((1000 < n) && (n < 2001)) a2++;
if((666 < n) && (n < 1001)) a3++;
if((500 < n) && (n < 667)) a4++;
if((400 < n) && (n < 501)) a5++;
if((333 < n) && (n < 401)) a6++;
if((285 < n) && (n < 334)) a7++;
if((250 < n) && (n < 286)) a8++;
if((222 < n) && (n < 251)) a9++;
if((200 < n) && (n < 223)) a10++;
if((181 < n) && (n < 201)) a11++;
if((166 < n) && (n < 182)) a12++;
if((153 < n) && (n < 167)) a13++;
if((142 < n) && (n < 154)) a14++;
if((133 < n) && (n < 143)) a15++;
if((125 < n) && (n < 134)) a16++;
if((117 < n) && (n < 126)) a17++;
if((111 < n) && (n < 118)) a18++;
if((105 < n) && (n < 112)) a19++;
if((100 < n) && (n < 106)) a20++;
n = 0;
}
Delay_1mS;
}
bufn[1]=(a1%10)+'0'; a1=a1/10; bufn[0]=(a1%10)+'0'; bufn[2]=' ';
bufn[4]=(a2%10)+'0'; a2=a2/10; bufn[3]=(a2%10)+'0'; bufn[5]=' ';
bufn[7]=(a3%10)+'0'; a3=a3/10; bufn[6]=(a3%10)+'0'; bufn[8]=' ';
bufn[10]=(a4%10)+'0'; a4=a4/10; bufn[9]=(a4%10)+'0'; bufn[11]=' ';
bufn[13]=(a5%10)+'0'; a5=a5/10; bufn[12]=(a5%10)+'0'; bufn[14]=' ';
bufn[16]=(a6%10)+'0'; a6=a6/10; bufn[15]=(a6%10)+'0'; bufn[17]=' ';
bufn[19]=(a7%10)+'0'; a7=a7/10; bufn[18]=(a7%10)+'0'; bufn[20]=' ';
bufn[22]=(a8%10)+'0'; a8=a8/10; bufn[21]=(a8%10)+'0'; bufn[23]=' ';
bufn[25]=(a9%10)+'0'; a9=a9/10; bufn[24]=(a9%10)+'0'; bufn[26]=' ';
bufn[28]=(a10%10)+'0'; a10=a10/10; bufn[27]=(a10%10)+'0'; bufn[29]=' ';
bufn[31]=(a11%10)+'0'; a11=a11/10; bufn[30]=(a11%10)+'0'; bufn[32]=' ';
bufn[34]=(a12%10)+'0'; a12=a12/10; bufn[33]=(a12%10)+'0'; bufn[35]=' ';
bufn[37]=(a13%10)+'0'; a13=a13/10; bufn[36]=(a13%10)+'0'; bufn[38]=' ';
bufn[40]=(a14%10)+'0'; a14=a14/10; bufn[39]=(a14%10)+'0'; bufn[41]=' ';
bufn[43]=(a15%10)+'0'; a15=a15/10; bufn[42]=(a15%10)+'0'; bufn[44]=' ';
bufn[46]=(a16%10)+'0'; a16=a16/10; bufn[45]=(a16%10)+'0'; bufn[47]=' ';
bufn[49]=(a17%10)+'0'; a17=a17/10; bufn[48]=(a17%10)+'0'; bufn[50]=' ';
bufn[52]=(a18%10)+'0'; a18=a18/10; bufn[51]=(a18%10)+'0'; bufn[53]=' ';
bufn[55]=(a19%10)+'0'; a19=a19/10; bufn[54]=(a19%10)+'0'; bufn[56]=' ';
bufn[58]=(a20%10)+'0'; a20=a20/10; bufn[57]=(a20%10)+'0'; bufn[59]=' ';
bufn[60] = '\n';
for(ssw=0; ssw<2; ssw++){
if(mUSBUSARTIsTxTrfReady()) putUSBUSART(bufn, 61);
CDCTxService();
}
break;
default:
break;
}
}
}
//* ボーレート変更関数 USB側とUSARTの通信速度が同じでなければならない/
#if defined(USB_CDC_SET_LINE_CODING_HANDLER)
void mySetLineCodingHandler(void){
//If the request is not in a valid range
if(cdc_notice.GetLineCoding.dwDTERate.Val > 115200)
{ // 115kbps以上なら何もしない
}
else
{
DWORD_VAL dwBaud;
// CDCドライバのボーレート変更
CDCSetBaudRate(cdc_notice.GetLineCoding.dwDTERate.Val);
// USARTのボーレート変更
dwBaud.Val = (DWORD)(GetSystemClock()/4)/line_coding.dwDTERate.Val-1;
SPBRG = dwBaud.v[0];
SPBRGH = dwBaud.v[1];
}
}
#endif
// (ここから下は変更無し)
void USBCBSuspend(void)
{
#if defined(__C30__)
#if 0
U1EIR = 0xFFFF;
U1IR = 0xFFFF;
U1OTGIR = 0xFFFF;
IFS5bits.USB1IF = 0;
IEC5bits.USB1IE = 1;
U1OTGIEbits.ACTVIE = 1;
U1OTGIRbits.ACTVIF = 1;
Sleep();
#endif
#endif
}
#if 0
void __attribute__ ((interrupt)) _USB1Interrupt(void)
{
#if !defined(self_powered)
if(U1OTGIRbits.ACTVIF) {
IEC5bits.USB1IE = 0;
U1OTGIEbits.ACTVIE = 0;
IFS5bits.USB1IF = 0;
//USBClearInterruptFlag(USBActivityIFReg,USBActivityIFBitNum);
USBClearInterruptFlag(USBIdleIFReg,USBIdleIFBitNum);
//USBSuspendControl = 0;
}
#endif
}
#endif
void USBCBWakeFromSuspend(void)
{
}
void USBCB_SOF_Handler(void)
{
}
void USBCBErrorHandler(void)
{
}
void USBCBCheckOtherReq(void)
{
USBCheckCDCRequest();
}
void USBCBStdSetDscHandler(void)
{
}
void USBCBInitEP(void)
{
CDCInitEP();
}
void USBCBSendResume(void)
{
static WORD delay_count;
USBResumeControl = 1; // Start RESUME signaling
delay_count = 1800U; // Set RESUME line for 1-13 ms
do {
delay_count--;
} while(delay_count);
USBResumeControl = 0;
}
#if defined(ENABLE_EP0_DATA_RECEIVED_CALLBACK)
void USBCBEP0DataReceived(void)
{
}
#endif
BOOL USER_USB_CALLBACK_EVENT_HANDLER(USB_EVENT event, void *pdata, WORD size)
{
switch(event) {
case EVENT_CONFIGURED:
USBCBInitEP();
break;
case EVENT_SET_DESCRIPTOR:
USBCBStdSetDscHandler();
break;
case EVENT_EP0_REQUEST:
USBCBCheckOtherReq();
break;
case EVENT_SOF:
USBCB_SOF_Handler();
break;
case EVENT_SUSPEND:
USBCBSuspend();
break;
case EVENT_RESUME:
USBCBWakeFromSuspend();
break;
case EVENT_BUS_ERROR:
USBCBErrorHandler();
break;
case EVENT_TRANSFER:
Nop();
break;
default:
break;
}
return TRUE;
}